define(['../module','../../services/anon/authService','../../services/anon/user_service'],
    function (controllers) {
        "use strict";

        controllers.controller('registerCtrl', ['$scope', '$http', 'toaster', '$state','IsBackEndOk','userFactory', function ($scope, $http, toaster, $state,IsBackEndOk,userFactory) {

    $scope.registerStatus = false;
    $scope.vertifyStatus = false;


    //$scope.sendMailProcess = false;
    //$scope.sendmailEnd =false;

    $scope.user = {
        roles: ['user']
    };

    $scope.term =  true;

    $scope.register = function (formInstance) {
        console.log(formInstance);
        if(IsBackEndOk == 'true')
        {
            console.log("true");
            var user = {
                "user":formInstance
            };

            userFactory.insertUser(user)
                .success(function () {
                    $scope.status = 'Inserted User! Refreshing.';
                })
                .error(function(error) {
                    $scope.status = 'Unable to insert User: ' + error.message;
                });
        }else{
            $scope.processing = true;

            $scope.auth.register(
                $scope.user,
                function(data, status, headers, config){
                    console.info('post success - ', data);
                    $scope.processing = false;
                    $scope.registerStatus = true;
                },

                function(data, status, headers, config){
                    data.errors.forEach(function (error, index, array) {
                        formInstance[error.field].$error[error.name] = true;
                    });
                    formInstance.$setPristine();
                    console.info('post error - ', data);
                    toaster.pop('error','用户名已注册！');
                    $scope.processing = false;
                }
            );
        }

    };

    $scope.emailVertifyStatus = false;
    //邮箱验证
    $scope.emailVertifyPro = function (formInstance) {
        $scope.emailVertifyProcessing = true;

        $scope.auth.emailVertify(
            {   user:$scope.user,
                vertify:$scope.emailV
            },
            function(res){
                $scope.emailVertifyProcessing = false;
                $scope.emailVertifyStatus = true;
            },
            function(data, status, headers, config){
                data.errors.forEach(function (error, index, array) {
                    formInstance[error.field].$error[error.name] = true;
                });
                formInstance.$setPristine();
                console.info('post error - ', data);
                toaster.pop('error','验证出错！');
                $scope.emailVertifyProcessing = false;
                $scope.emailVertifyStatus = false;
            }
        )
    };

}])
});